package company

import (
	"context"
	v1 "wh-1-backend/api/v1"
	"wh-1-backend/internal/dao"
	"wh-1-backend/internal/model"
	"wh-1-backend/internal/model/do"
	"wh-1-backend/internal/model/entity"
	"wh-1-backend/internal/service"
)

type sCompany struct{}

func New() *sCompany {
	return &sCompany{}
}
func init() {
	service.RegisterCompany(New())
}

func (c *sCompany) ListByConditionWithPage(ctx context.Context, p model.PageInput, cond model.CompanyPageListCondition) (total int, res []*v1.SimpleCompanyList, err error) {
	m := dao.Company.Ctx(ctx)
	if cond.Title != "" {
		m = m.WhereLike(dao.Company.Columns().Title, cond.Title+"%")
	}
	total, err = m.Count()
	if err != nil || total == 0 {
		return
	}
	err = m.Page(p.Page, p.PageSize).Scan(&res)
	if err != nil {
		return
	}
	var ids []uint
	for _, re := range res {
		if re.IndustryId > 0 {
			ids = append(ids, re.IndustryId)
		}
	}
	if len(ids) > 0 {
		industryList, err := service.Industry().ListByIds(ctx, ids)
		if err == nil {
			var industryKeyList = make(map[uint]*entity.Industry)
			for _, industry := range industryList {
				industryKeyList[industry.Id] = industry
			}
			for _, re := range res {
				if industry, ok := industryKeyList[re.IndustryId]; ok {
					re.Industry = industry
				}
			}
		}
	}
	return
}

func (c *sCompany) List(ctx context.Context) (res []*v1.SimpleCompanyList, err error) {
	err = dao.Company.Ctx(ctx).Scan(&res)
	return
}

func (c *sCompany) ListByIds(ctx context.Context, ids []uint) (res []*v1.SimpleCompanyList, err error) {
	err = dao.Company.Ctx(ctx).WhereIn(dao.Company.Columns().Id, ids).Scan(&res)
	return
}

func (c *sCompany) Save(ctx context.Context, input model.EditCompanyInput) (err error) {
	_, err = dao.Company.Ctx(ctx).OmitNil().Save(do.Company{
		Title:            input.Title,
		Host:             input.Host,
		Address:          input.Address,
		Email:            input.Email,
		FinanceId:        input.FinanceId,
		IndustryId:       input.IndustryId,
		ClientDes:        input.ClientDes,
		Culture:          input.Culture,
		InterviewProcess: input.InterviewProcess,
		PrivyDes:         input.PrivyDes,
	})
	return
}

func (c *sCompany) Update(ctx context.Context, id uint, input model.EditCompanyInput) (err error) {
	_, err = dao.Company.Ctx(ctx).
		Where(dao.Company.Columns().Id, id).
		Update(do.Company{
			Title:            input.Title,
			Host:             input.Host,
			Address:          input.Address,
			Email:            input.Email,
			FinanceId:        input.FinanceId,
			IndustryId:       input.IndustryId,
			ClientDes:        input.ClientDes,
			Culture:          input.Culture,
			InterviewProcess: input.InterviewProcess,
			PrivyDes:         input.PrivyDes,
		})
	return
}

func (c *sCompany) Delete(ctx context.Context, id uint) (err error) {
	_, err = dao.Company.Ctx(ctx).Where(dao.Company.Columns().Id, id).Delete()
	return
}

func (c *sCompany) GetById(ctx context.Context, id uint) (res *entity.Company, err error) {
	err = dao.Company.Ctx(ctx).Where(dao.Company.Columns().Id, id).Scan(&res)
	return
}

func (c *sCompany) GetByTitle(ctx context.Context, title string) (res *entity.Company, err error) {
	err = dao.Company.Ctx(ctx).Where(dao.Company.Columns().Title, title).Limit(1).Scan(&res)
	return
}

func (c *sCompany) CheckAdnCreateByTitle(ctx context.Context, title string) (res *entity.Company, err error) {
	res, err = c.GetByTitle(ctx, title)
	if err != nil {
		return
	}
	if res == nil {
		var id int64
		id, err = dao.Company.Ctx(ctx).InsertAndGetId(do.Company{Title: title})
		if err != nil {
			return
		}
		res = &entity.Company{Id: uint(id)}
	}
	return
}
